package com.dream.fragment;


import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.dream.classcontrol.MainActivity;
import com.dream.classcontrol.R;
import com.dream.config.DevUIIDConfig;
import com.dream.sdkmanager.SDKManager;


public class ChuanglianFragment extends android.app.Fragment implements View.OnClickListener {

    private ImageView chuanglianOne;
    private ImageView chuanglianTwo;
    private ImageView chuanglianThree;
    private ImageView chuanglianFour;
    private ImageView chuanglianOneOpen;
    private ImageView chuanglianOneClose;
    private ImageView chuanglianTwoOpen;
    private ImageView chuanglianTwoClose;
    private ImageView chuanglianThreeOpen;
    private ImageView chuanglianThreeClose;
    private ImageView chuanglianFourOpen;
    private ImageView chuanglianFourClose;

    private ImageView[] imageViewsOpen;
    private ImageView[] imageViewsClose;

    private MainActivity mainActivity;
    private SDKManager sdkManager;

    public ChuanglianFragment() {

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mainActivity= (MainActivity) activity;
        sdkManager=mainActivity.getSdkManager();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_chuanglian, container, false);
        initView(view);
        setOnClick();
        return view;
    }

    public void initView(View view){
        chuanglianOne = (ImageView) view.findViewById(R.id.chuanglian_one);
        chuanglianTwo = (ImageView) view.findViewById(R.id.chuanglian_two);
        chuanglianThree = (ImageView) view.findViewById(R.id.chuanglian_three);
        chuanglianFour = (ImageView) view.findViewById(R.id.chuanglian_four);
        chuanglianOneOpen = (ImageView) view.findViewById(R.id.chuanglian_one_open);
        chuanglianOneClose = (ImageView) view.findViewById(R.id.chuanglian_one_close);
        chuanglianTwoOpen = (ImageView) view.findViewById(R.id.chuanglian_two_open);
        chuanglianTwoClose = (ImageView) view.findViewById(R.id.chuanglian_two_close);
        chuanglianThreeOpen = (ImageView) view.findViewById(R.id.chuanglian_three_open);
        chuanglianThreeClose = (ImageView) view.findViewById(R.id.chuanglian_three_close);
        chuanglianFourOpen = (ImageView) view.findViewById(R.id.chuanglian_four_open);
        chuanglianFourClose = (ImageView) view.findViewById(R.id.chuanglian_four_close);

        imageViewsOpen=new ImageView[]{chuanglianOneOpen,chuanglianTwoOpen,chuanglianThreeOpen,chuanglianFourOpen};
        imageViewsClose=new ImageView[]{chuanglianOneClose,chuanglianTwoClose,chuanglianThreeClose,chuanglianFourClose};
    }

    public void setOnClick(){
        chuanglianOneOpen.setOnClickListener(this);
        chuanglianOneClose.setOnClickListener(this);
        chuanglianTwoOpen.setOnClickListener(this);
        chuanglianTwoClose.setOnClickListener(this);
        chuanglianThreeOpen.setOnClickListener(this);
        chuanglianThreeClose.setOnClickListener(this);
        chuanglianFourOpen.setOnClickListener(this);
        chuanglianFourClose.setOnClickListener(this);
    }

    public void controlDev(String devId,Integer state){
        if (sdkManager.getLandingSate()){
            sdkManager.ControlDev(DevUIIDConfig.chuanglian,devId,state);
        }else {
            Toast.makeText(getActivity(),"请登录后重试",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.chuanglian_one_open:
                setTextViewBg(0,"on");
                chuanglianOne.setImageResource(R.drawable.icon_chuanglian_one_selected);
                controlDev("devId_1",1);
                break;
            case R.id.chuanglian_one_close:
                setTextViewBg(0,"off");
                chuanglianOne.setImageResource(R.drawable.icon_chuanglian_one_unselect);
                controlDev("devId_1",0);
                break;
            case R.id.chuanglian_two_open:
                setTextViewBg(1,"on");
                chuanglianTwo.setImageResource(R.drawable.icon_chuanglian_two_selected);
                controlDev("devId_2",1);
                break;
            case R.id.chuanglian_two_close:
                setTextViewBg(1,"off");
                chuanglianTwo.setImageResource(R.drawable.icon_chuanglian_two_unselect);
                controlDev("devId_2",0);
                break;
            case R.id.chuanglian_three_open:
                setTextViewBg(2,"on");
                chuanglianThree.setImageResource(R.drawable.icon_chuanglian_three_selected);
                controlDev("devId_3",1);
                break;
            case R.id.chuanglian_three_close:
                setTextViewBg(2,"off");
                chuanglianThree.setImageResource(R.drawable.icon_chuanglian_three_unselect);
                controlDev("devId_3",0);
                break;
            case R.id.chuanglian_four_open:
                setTextViewBg(3,"on");
                chuanglianFour.setImageResource(R.drawable.icon_chuanglian_four_selected);
                controlDev("devId_4",1);
                break;
            case R.id.chuanglian_four_close:
                setTextViewBg(3,"off");
                chuanglianFour.setImageResource(R.drawable.icon_chuanglian_four_unselect);
                controlDev("devId_4",0);
                break;
        }
    }

    public void setTextViewBg(int index,String state){
        if (state.equals("on")){
            imageViewsOpen[index].setImageResource(R.drawable.bg_dengguan_open_selected);
            imageViewsClose[index].setImageResource(R.drawable.bg_dengguan_close_unselect);
        }else {
            imageViewsOpen[index].setImageResource(R.drawable.bg_dengguan_open_unselect);
            imageViewsClose[index].setImageResource(R.drawable.bg_dengguan_close_selected);
        }
    }

}
